package com.classeven.downhill.state;

import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.scene.Scene;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.detector.ClickDetector;

import android.view.KeyEvent;

import com.classeven.downhill.define.iGameDefines;
import com.classeven.downhill.element.ABackground;
import com.classeven.downhill.element.AScene;
import com.classeven.downhill.element.ASprite;
import com.classeven.downhill.engine.GameEngine;
import com.classeven.downhill.engine.Menu_Scroll_Y;
import com.classeven.downhill.engine.Utils;

public class State_Select_Arena {

	GameEngine engine;
	ABackground bg_Arena;
	ASprite arena1, arena2;
	ASprite back;
	AScene scene;
	Menu_Scroll_Y scroll;
	
	public State_Select_Arena(GameEngine engine)
	{
		this.engine = engine;
		
		bg_Arena = new ABackground(engine, iGameDefines.SPRITE_ARENA, 0);
		arena1 = new ASprite(engine, iGameDefines.SPRITE_ARENA, 1);
		arena2 = new ASprite(engine, iGameDefines.SPRITE_ARENA, 2);
		back = new ASprite(engine, iGameDefines.SPRITE_ARENA, 3);
		
		scroll = new Menu_Scroll_Y(engine, 0, arena1.theSprite(), arena2.theSprite());
		scene = new AScene(0);
		
		
	}
	
	public void init()
	{
		setPos();
		
		engine.SetGameScene(scene);
		engine.SceneAttach(scene, bg_Arena);
		engine.SceneAttach(scene, arena1);
		engine.SceneAttach(scene, arena2);
		engine.SceneAttach(scene, back);
		
		scene.theScene().setOnAreaTouchListener(engine);
		scene.theScene().setOnSceneTouchListener(engine);
		
		scene.theScene().registerTouchArea(back.theSprite());
		
//		scene.theScene().registerTouchArea(arena1.theSprite());
		
		
	}
		
	private void setPos() 
	{
		arena1.theSprite().setAlpha(0.8f);
		arena2.theSprite().setAlpha(0.8f);
		
	}

	public void update()
	{
//		Utils.TRACE("Fokus = "+scroll.getFocus());
//		Utils.TRACE("Scale = "+arena1.theSprite().getScaleY());
	}
	
	public void exit()
	{
		scene.theScene().detachChildren();
	}

	public void sceneTouch(Scene pScene, TouchEvent pSceneTouchEvent) 
	{
			scroll.onTouchEvent(pSceneTouchEvent);
		
	}

	public void back(int keyCode, KeyEvent event)
	{
		if(keyCode == KeyEvent.KEYCODE_BACK)
		{
			exit();
			engine.SetNextMessageState(0);
			engine.SetNextGameState(iGameDefines.GAME_STATE_MAIN_MENU);
		}
		
	}

	public void event(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea,
			float pTouchAreaLocalX, float pTouchAreaLocalY)
	{
		
		if(pTouchArea.equals(back.theSprite()) && pSceneTouchEvent.isActionDown())
		{
			exit();
			engine.SetNextMessageState(0);
			engine.SetNextGameState(iGameDefines.GAME_STATE_MAIN_MENU);
		}
		
		
	}
	

	public void click(ClickDetector pClickDetector, int pPointerID,
			float pSceneX, float pSceneY)
	{
		if(Utils.isOnArea(pSceneX, pSceneY, arena1.theSprite()) && 
				scroll.getFocus() == 0 && arena1.theSprite().getScaleY() == 1 && arena1.theSprite().getAlpha() != 1)
		{
//			exit();
//			engine.SetNextMessageState(0);
//			engine.SetNextGameState(iGameDefines.GAME_STATE_GAME_PLAY);
			
			arena1.theSprite().setAlpha(1f);
			
		}
		else if(Utils.isOnArea(pSceneX, pSceneY, arena1.theSprite()) && arena1.theSprite().getAlpha() == 1)
		{
			exit();
			engine.SetNextMessageState(0);
			engine.SetNextGameState(iGameDefines.GAME_STATE_GAME_PLAY);
			Utils.TRACE("CKICKCICKC");
		}
	}
}
